package com.ruoyi.biz.letter.controller;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.ruoyi.biz.export.enums.ExportTemplateEnum;
import com.ruoyi.biz.imported.enums.ImportedTemplateEnum;
import com.ruoyi.biz.letter.domain.BizLeaderMailbox;
import com.ruoyi.biz.letter.export.BizCityLetterExport;
import com.ruoyi.biz.letter.util.AcceptDeptUtil;
import com.ruoyi.biz.letter.util.CategoryUtil;
import com.ruoyi.common.enums.LetterStatus;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.letter.domain.BizCityLetter;
import com.ruoyi.biz.letter.service.IBizCityLetterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 地市信访Controller
 *
 * @author Fei.Cong
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/city/letter")
public class BizCityLetterController extends BaseController {

    @Autowired
    private IBizCityLetterService bizCityLetterService;

    /**
     * 查询地市信访列表
     */
    @PreAuthorize("@ss.hasPermi('city:letter:list')")
    @GetMapping("/list")
    public AjaxResult list(BizCityLetter bizCityLetter) {
        LambdaQueryWrapper<BizCityLetter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizCityLetterService.list(lambdaQueryWrapper));
    }

    /**
     * 分页查询地市信访列表
     */
    @PreAuthorize("@ss.hasPermi('city:letter:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizCityLetter bizCityLetter) {
        CategoryUtil.handleQueryCategoryId(bizCityLetter);
        AcceptDeptUtil.handleQueryAcceptDept(bizCityLetter);
        IPage iPage;
        switch (bizCityLetter.getExportType()) {
            case "DISPATCH":
            case "CLAIM":
                iPage = bizCityLetterService.pageList(bizCityLetter, startPage());
                break;
            default:
                iPage = bizCityLetterService.noPermissionPageList(bizCityLetter, startPage());
                break;
        }
        return getDataTable(iPage);
    }

    /**
     * 导出地市信访列表
     */
    @PreAuthorize("@ss.hasPermi('city:letter:export')")
    @Log(title = "地市信访", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizCityLetter bizCityLetter) {
        List<BizCityLetter> list = bizCityLetterService.queryList(bizCityLetter);
        ExcelUtil<BizCityLetter> util = new ExcelUtil<BizCityLetter>(BizCityLetter.class);
        util.exportExcel(response, list, "地市信访数据");
    }

    /**
     * 获取地市信访详细信息
     */
    @PreAuthorize("@ss.hasPermi('city:letter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BizCityLetter bizCityLetter = bizCityLetterService.getById(id);
        CategoryUtil.getCategoryIds(bizCityLetter);
        AcceptDeptUtil.getAcceptDept(bizCityLetter);
        return success(bizCityLetter);
    }

    /**
     * 新增地市信访
     */
    @PreAuthorize("@ss.hasPermi('city:letter:add')")
    @Log(title = "地市信访", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizCityLetter bizCityLetter) {
        return toAjax(bizCityLetterService.save(bizCityLetter));
    }

    /**
     * 修改地市信访
     */
//    @PreAuthorize("@ss.hasPermi('city:letter:edit')")
    @Log(title = "地市信访", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCityLetter bizCityLetter) {
        bizCityLetterService.update(bizCityLetter);
        return success();
    }

    /**
     * 退回地市信访
     */
//    @PreAuthorize("@ss.hasPermi('city:letter:edit')")
    @Log(title = "地市信访退回", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/back/{id}")
    public AjaxResult back(@PathVariable("id") Long id) {
        bizCityLetterService.back(id);
        return success();
    }

    /**
     * 删除地市信访
     */
    @PreAuthorize("@ss.hasPermi('city:letter:remove')")
    @Log(title = "地市信访", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizCityLetterService.removeBatchByIds(ids));
    }

    @GetMapping(value = "/checkRepeatClaim/{id}")
    public AjaxResult checkRepeatClaim(@PathVariable("id") Long id) {
        return success(bizCityLetterService.checkRepeatClaim(id));
    }

    @PreAuthorize("@ss.hasPermi('city:letter:reset')")
    @Log(title = "重置地市信访", businessType = BusinessType.UPDATE)
    @GetMapping("/reset/{ids}")
    public AjaxResult reset(@PathVariable List<Long> ids) {
        bizCityLetterService.reset(ids);
        return success();
    }

}